/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.view.settings.report;

import java.awt.Font;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFileChooser;
import say.swing.JFontChooser;
import systole.propertiesController.ReportSettingsProperties;
import systole.utils.ImageUtils;
import systole.view.abstractController.AbstractEditViewController;

/**
 * 
 * @author jmj
 */
public class ControllerReportSettings extends AbstractEditViewController{

    private JDialogReportSettings reportSettings;
    private ReportSettingsProperties properties;

    /**
     * @param parent
     */
    public ControllerReportSettings(java.awt.Frame parent) {
        super();
        this.reportSettings = new JDialogReportSettings(parent, this);
        this.properties = new ReportSettingsProperties();
        this.setCloseListener(reportSettings);
    }

    /**
     * 
     */
    public void showSettings() {
        this.loadPropertiesOnForm();
        this.reportSettings.setVisible(true);
    }

    /**
     * 
     */
    public void ok() {
        this.loadPropertiesFromForm();
        this.properties.saveProperties();
        this.reportSettings.setVisible(false);
    }

    public void cancel() {
        this.reportSettings.setVisible(false);
    }

    private void loadPropertiesFromForm() {
        // page 1
        this.properties.saveTitle(this.reportSettings.getjTxtTitle().getText());
        this.properties.saveTitleFont(this.reportSettings.getjTxtTitle().getFont());
        this.properties.saveUseLeftImage(this.reportSettings.getjChkImageLeft().isSelected());
        this.properties.saveUseRightImage(this.reportSettings.getjChkImageRight().isSelected());
        this.properties.saveLeftImageSize(this.reportSettings.getjCmbLeftImageSize().getSelectedIndex());
        this.properties.saveRightImageSize(this.reportSettings.getjCmbRightImageSize().getSelectedIndex());

        // page 2
        this.properties.saveIndexFont(this.reportSettings.getjTxtIndexFont().getFont());
        this.properties.saveIndexValueFont(this.reportSettings.getjTxtIndexValueFont().getFont());
        this.properties.saveDataFont(this.reportSettings.getjTxtDataFont().getFont());
        this.properties.saveDataValueFont(this.reportSettings.getjTxtDataValueFont().getFont());
        this.properties.saveUseAOD(this.reportSettings.getjChkAOD().isSelected());
        this.properties.saveUseAOS(this.reportSettings.getjChkAOS().isSelected());
        this.properties.saveUseIAR(this.reportSettings.getjChkIAR().isSelected());
        this.properties.saveUseIMC(this.reportSettings.getjChkIMC().isSelected());
        this.properties.saveUseRS(this.reportSettings.getjChkRS().isSelected());
        this.properties.saveUseFC(this.reportSettings.getjChkFC().isSelected());
        this.properties.saveUseHeight(this.reportSettings.getjChkHeigt().isSelected());
        this.properties.saveUseWeight(this.reportSettings.getjChkWeigth().isSelected());
        this.properties.saveUseSmoke(this.reportSettings.getjChkSmooke().isSelected());
        this.properties.saveUseAge(this.reportSettings.getjChkAge().isSelected());
        this.properties.saveUsePD(this.reportSettings.getjChkPD().isSelected());
        this.properties.saveUsePS(this.reportSettings.getjChkPS().isSelected());

        // page 3
        this.properties.savePlotTitle(this.reportSettings.getjTxtPlotTitle().getText());
        this.properties.savePlotFont(this.reportSettings.getjTxtPlotTitle().getFont());
        this.properties.saveUseColorOnPlot(this.reportSettings.getjRbColor().isSelected());
        this.properties.savePlotTitleLocation(this.reportSettings.getjCmbLocation().getSelectedIndex());

        // page 4

        this.properties.saveBodyTextTitleFont(this.reportSettings.getjTxtBodyTitle().getFont());
        this.properties.saveBodyTextFont(this.reportSettings.getjTxtBodyText().getFont());

        this.saveImages();


    }

    private void loadPropertiesOnForm() {
        // page 1
        this.reportSettings.getjTxtTitle().setFont(
                this.properties.loadTitleFont());
        this.reportSettings.getjTxtTitle().setText(this.properties.loadTitle());
        this.reportSettings.getjChkImageLeft().setSelected(
                this.properties.loadUseLeftImage());
        this.reportSettings.getjXImageLeft().setEnabled(this.reportSettings.getjChkImageLeft().isSelected());
        this.reportSettings.getjBtnImageLeft().setEnabled(this.reportSettings.getjChkImageLeft().isSelected());
        this.reportSettings.getjCmbLeftImageSize().setEnabled(this.reportSettings.getjChkImageLeft().isSelected());

        this.reportSettings.getjChkImageRight().setSelected(
                this.properties.loadUseRightImage());

        this.reportSettings.getjXImageRight().setEnabled(this.reportSettings.getjChkImageRight().isSelected());
        this.reportSettings.getjBtnImageRight().setEnabled(this.reportSettings.getjChkImageRight().isSelected());
        this.reportSettings.getjCmbRightImageSize().setEnabled(this.reportSettings.getjChkImageRight().isSelected());

        this.reportSettings.getjCmbLeftImageSize().setSelectedIndex(
                this.properties.loadLeftImageSize());
        this.reportSettings.getjCmbRightImageSize().setSelectedIndex(
                this.properties.loadRightImageSize());

        // page 2

        this.reportSettings.getjTxtIndexFont().setFont(
                this.properties.loadIndexFont());
        this.reportSettings.getjTxtIndexValueFont().setFont(
                this.properties.loadIndexValueFont());
        this.reportSettings.getjTxtDataFont().setFont(
                this.properties.loadDataFont());
        this.reportSettings.getjTxtDataValueFont().setFont(
                this.properties.loadDataValueFont());
        this.reportSettings.getjChkAOD().setSelected(this.properties.loadUseAOD());
        this.reportSettings.getjChkAOS().setSelected(this.properties.loadUseAOS());
        this.reportSettings.getjChkIAR().setSelected(this.properties.loadUseIAR());
        this.reportSettings.getjChkIMC().setSelected(this.properties.loadUseIMC());
        this.reportSettings.getjChkRS().setSelected(this.properties.loadUseRS());
        this.reportSettings.getjChkFC().setSelected(this.properties.loadUseFC());
        this.reportSettings.getjChkHeigt().setSelected(this.properties.loadUseHeight());
        this.reportSettings.getjChkWeigth().setSelected(this.properties.loadUseWeight());
        this.reportSettings.getjChkSmooke().setSelected(this.properties.loadUseSmoke());
        this.reportSettings.getjChkAge().setSelected(this.properties.loadUseAge());
        this.reportSettings.getjChkPD().setSelected(this.properties.loadUsePD());
        this.reportSettings.getjChkPS().setSelected(this.properties.loadUsePS());


        // page 3
        this.reportSettings.getjTxtPlotTitle().setFont(
                this.properties.loadPlotFont());
        this.reportSettings.getjTxtPlotTitle().setText(
                this.properties.loadPlotTitle());
        this.reportSettings.getjRbColor().setSelected(this.properties.loadUseColorOnPlot());
        this.reportSettings.getjCmbLocation().setSelectedIndex(this.properties.loadPlotTitleLocation());

        // page 4

        this.reportSettings.getjTxtBodyTitle().setFont(
                this.properties.loadBodyTextTitleFont());
        this.reportSettings.getjTxtBodyText().setFont(
                this.properties.loadBodyTextFont());


        this.loadImages();
    }

    /**
     * @param curretnFont
     * @return font selected
     */
    public Font selectFont(Font curretnFont) {
        JFontChooser jfc = new JFontChooser();
        if (curretnFont != null) {
            jfc.setSelectedFont(curretnFont);
        }
        int result = jfc.showDialog(this.reportSettings);
        return ((result == JFontChooser.OK_OPTION) ? jfc.getSelectedFont()
                : curretnFont);
    }

    /**
     * @param width
     * @param height
     * @return image selected
     */
    public BufferedImage selectImage(int width, int height) {
        JFileChooser fc = new JFileChooser();
        fc.addChoosableFileFilter(new ImageFileFilter());
        if (fc.showOpenDialog(this.reportSettings) == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            try {
                BufferedImage img = ImageIO.read(file);
                img = ImageUtils.resizeImage(img, img.getType(), width, height);
                return img;
            } catch (IOException ex) {
                return null;
            }
        }
        return null;
    }

    private void saveImages() {

        if (this.reportSettings.getjChkImageLeft().isSelected()) {
            Image imgLeft = this.reportSettings.getjXImageLeft().getImage();
            if (imgLeft != null) {
                ImageUtils.saveImage(imgLeft, ImageUtils.IMAGES_FOLDER
                        + System.getProperty("file.separator") + ReportSettingsProperties.FILE_NAME_LEFT_IMAGE, "png");
            } else {
                this.properties.saveUseLeftImage(false);
            }
        }

        if (this.reportSettings.getjChkImageRight().isSelected()) {
            Image imgRight = this.reportSettings.getjXImageRight().getImage();
            if (imgRight != null) {
                ImageUtils.saveImage(imgRight, ImageUtils.IMAGES_FOLDER
                        + System.getProperty("file.separator") + ReportSettingsProperties.FILE_NAME_RIGHT_IMAGE, "png");
            } else {
                this.properties.saveUseRightImage(false);
            }
        }
    }

    private void loadImages() {

        if (this.reportSettings.getjChkImageLeft().isSelected()) {
            BufferedImage imgLeft = ImageUtils.loadImage(ImageUtils.IMAGES_FOLDER
                    + System.getProperty("file.separator") + ReportSettingsProperties.FILE_NAME_LEFT_IMAGE);
            if (imgLeft != null) {
                this.reportSettings.getjXImageLeft().setImage(imgLeft);
            } else {
                this.reportSettings.getjChkImageLeft().setSelected(false);
                this.reportSettings.getjXImageLeft().setEnabled(false);
                this.reportSettings.getjBtnImageLeft().setEnabled(false);
                this.reportSettings.getjCmbLeftImageSize().setEnabled(false);
            }
        }

        if (this.reportSettings.getjChkImageRight().isSelected()) {
            BufferedImage imgRight = ImageUtils.loadImage(ImageUtils.IMAGES_FOLDER
                    + System.getProperty("file.separator") + ReportSettingsProperties.FILE_NAME_RIGHT_IMAGE);
            if (imgRight != null) {
                this.reportSettings.getjXImageRight().setImage(imgRight);
            } else {
                this.reportSettings.getjChkImageRight().setSelected(false);
                this.reportSettings.getjXImageRight().setEnabled(false);
                this.reportSettings.getjBtnImageRight().setEnabled(false);
                this.reportSettings.getjCmbRightImageSize().setEnabled(false);
            }
        }

    }
}
